﻿using System.Collections.Generic;
using BoldBrick.OneIntranet.HR.DataObjects.EventReceiverObjects;
using BoldBrick.OneIntranet.HR.DataObjects.Lists.WorkflowsListData.Views;
using BoldBrick.SharePoint.CommonControls.DataObjects.Base;
using Microsoft.SharePoint;
using System.Linq;

namespace BoldBrick.OneIntranet.HR.DataObjects.Lists.WorkflowsListData
{
	/// <summary>
	/// Metadata class of "Workflow" list
	/// </summary>
	public class WorkflowsList : BaseList, IContentTypeUniqueOrderService
	{
		#region ------ Views ----------------------------------------------------------------------

		private WorkflowsView workflowsView;
		public WorkflowsView WorkflowsView
		{
			get
			{
				if (workflowsView == null)
					workflowsView = new WorkflowsView();
				return workflowsView;
			}
		}

		private WorkflowStepsView workflowStepsView;
		public WorkflowStepsView WorkflowStepsView
		{
			get
			{
				if (workflowStepsView == null)
					workflowStepsView = new WorkflowStepsView();
				return workflowStepsView;
			}
		}

		

		public override ViewsCollection GetViews()
		{
			ViewsCollection viewsCollection = new ViewsCollection
			                                  	{
			                                  		this.WorkflowsView,
													this.WorkflowStepsView
			                                  	};
			return viewsCollection;
		}

		#endregion

		public WorkflowsList()
		{
			this.EnableFolderCreation = false;
			this.EnableAttachments = false;
		}

		protected override string GetListTitleResourceKey()
		{
			return "List_Workflows_Title";
		}

		protected override string GetListNameResourceKey()
		{
			return "List_Workflows_Name";
		}

		protected override SPListTemplateType? GetListTemplateType()
		{
			return SPListTemplateType.GenericList;
		}

		public override List<BaseContentType> GetContentTypes()
		{
			List<BaseContentType> contentTypes = new List<BaseContentType>
			                                     	{
			                                     		ContentTypes.ContentTypes.WorkflowContentType,
			                                     		ContentTypes.ContentTypes.InterviewWorkflowStepContentType,
			                                     		ContentTypes.ContentTypes.ReferenceCallWorkflowStepContentType,
			                                     		ContentTypes.ContentTypes.TestWorkflowStepContentType,
			                                     		ContentTypes.ContentTypes.FinalDecisionWorkflowStepContentType,
			                                     		ContentTypes.ContentTypes.ReviewWorkflowStepContentType,
			                                     		ContentTypes.ContentTypes.ScheduleInterviewWorkflowStepContentType
			                                     	};
			return contentTypes;
		}

		public override List<BaseEventReceiverObject> GetEventReceivers()
		{
			List<BaseEventReceiverObject> eventReceivers = new List<BaseEventReceiverObject>
			                                               	{
			                                               		new WorkflowsListItemAddedEventReceiverObject()
			                                               	};
			return eventReceivers;
		}

		/// <summary>
		/// Setups order of content type in "New" menu item
		/// </summary>
		/// <param name="list">Parent list of the folder</param>
		/// <param name="folder">Folder to setup</param>
		public void SetupContentTypeUniqueOrder(SPList list, SPFolder folder)
		{
			SPContentType[] contentTypes = list.ContentTypes.OfType<SPContentType>()
				.Where(contentType => !contentType.Id.IsChildOf(ContentTypes.ContentTypes.WorkflowContentType.ID))
				.OrderBy(contentType => contentType.Name)
				.ToArray();

			folder.UniqueContentTypeOrder = contentTypes;
			folder.Update();

		}
	}
}
